/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.example.elemui_demo.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public final class MD5 {
	private static final Log log = LogFactory.getLog(MD5.class);
	private static final String ENCODE = "UTF-8";
	private static final String ALGORITHM = "MD5";

	private static final char[] md5(String text) {
		char[] value = (char[]) null;
		try {
			char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
					'9', 'A', 'B', 'C', 'D', 'E', 'F' };
			byte[] strTemp = text.getBytes("UTF-8");
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			value = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; ++i) {
				byte byte0 = md[i];
				value[(k++)] = hexDigits[(byte0 >>> 4 & 0xF)];
				value[(k++)] = hexDigits[(byte0 & 0xF)];
			}
		} catch (NoSuchAlgorithmException e) {
			log.error("执行加密算[MD5]法异常!", e);
		} catch (Exception e) {
			log.error(e);
		}

		return value;
	}

	public static final String md5for32(String text) {
		String ptext = new String(md5(text));
		if (log.isDebugEnabled()) {
			log.debug("密文：" + ptext + " 明文：" + text);
		}

		return ptext;
	}

	public static final String md5for16(String text) {
		String ptext = new String(md5(text)).substring(8, 24);
		if (log.isDebugEnabled()) {
			log.debug("密文：" + ptext + " 明文：" + text);
		}

		return ptext;
	}

	public static void main(String[] args) {
		//http://ah.189.cn/iphone/ext/fukawap/index.html?byChannel=wx-xxxx &cha=30000&phone=19965106184&time=20190816085238&encrypt=33F75192C0AA93DA8129F0F822C990BC
		String cha="30000" ; //（可以固定值30000）
		String phone="19965106184"; //手机号
		String time="20190816085238";

		System.out.println(MD5.md5for32(cha+phone+"weixin"+time).toUpperCase());
	}
}